package com.liduanwei.springboot.config.interceptor;

import com.jinrustar.op.core.util.WebUtil;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * 请求拦截器
 */
public class RequestInterceptor implements HandlerInterceptor {
    protected final static org.slf4j.Logger logger = LoggerFactory.getLogger(RequestInterceptor.class);


    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        Map requestParams = WebUtil.getParameterMap(httpServletRequest);
        String url = httpServletRequest.getRequestURL().toString();
        logger.info("请求拦截: method: {}, class: {},url:{}, params:{},ip:{}", method.getName(), method.getDeclaringClass().getSimpleName(), url, requestParams, WebUtil.getHost(httpServletRequest));
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
